/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.shengqin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shengqin.common.pojo.CommonPage;
import com.shengqin.config.CommonException;
import com.shengqin.entity.SysHealthProject;
import com.shengqin.mapper.SysHealthProjectMapper;
import com.shengqin.service.SysHealthProjectService;
import com.shengqin.utils.GradeUtil;
import com.shengqin.utils.ProjectUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 体测项目Service接口实现类
 *
 * @author Rui.Zhou
 * @date 2022/12/22 19:30
 **/
@Service
public class SysHealthProjectServiceImpl extends ServiceImpl<SysHealthProjectMapper, SysHealthProject> implements SysHealthProjectService {

    @Override
    public SysHealthProject listByPrjCodeAndGradeId(String prjCode, String gradeId, Integer templateId) {
        QueryWrapper<SysHealthProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysHealthProject::getGradeId, gradeId);
        queryWrapper.lambda().eq(SysHealthProject::getProjectName, prjCode);
        queryWrapper.lambda().eq(SysHealthProject::getTemplateId, templateId);
        List<SysHealthProject> projects = list(queryWrapper);
        return CollectionUtils.isEmpty(projects) ? null : projects.get(0);
    }

    @Override
    public Page<SysHealthProject> getPage(CommonPage<SysHealthProject> projectCommonPage) {
        QueryWrapper<SysHealthProject> queryWrapper = new QueryWrapper<>();
        SysHealthProject queryParam = projectCommonPage.getQueryParam();
        if (StringUtils.hasLength(queryParam.getCode())) {
            queryWrapper.lambda().eq(SysHealthProject::getCode, queryParam.getCode());
        }
        if (StringUtils.hasLength(queryParam.getGradeId())) {
            queryWrapper.lambda().eq(SysHealthProject::getGradeId, queryParam.getGradeId());
        }
        if(ObjectUtil.isNotEmpty(queryParam.getProjectName())) {
            queryWrapper.lambda().like(SysHealthProject::getProjectName, queryParam.getProjectName());
        }
        queryWrapper.lambda().eq(SysHealthProject::getTemplateId, queryParam.getTemplateId());
        queryWrapper.orderByAsc("grade_id");
        Page<SysHealthProject> projectPage = new Page<>(projectCommonPage.getPageNo(), projectCommonPage.getPageSize());
        page(projectPage, queryWrapper);
        buildRecords(projectPage.getRecords());
        return projectPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveData(List<SysHealthProject> cachedDataList, Integer templateId) {
        try{
            cachedDataList.forEach(healthProject -> {
                healthProject.setTemplateId(templateId);
                String projectCode = ProjectUtil.getPrjCode(healthProject.getProjectName());
                healthProject.setCode(projectCode);
            });

            saveOrUpdateBatch(cachedDataList);
        }catch (Exception e){
            log.error("导入项目报错",e);
            throw new CommonException("导入项目报错" + e.getMessage());
        }
    }

    @Override
    public List<SysHealthProject> listByTemplateId(Integer copyId) {
        QueryWrapper<SysHealthProject> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysHealthProject::getTemplateId, copyId);
        return list(queryWrapper);
    }

    @Override
    public void removeByTemplateId(Integer templateId) {
        QueryWrapper<SysHealthProject> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysHealthProject::getTemplateId, templateId);
        remove(wrapper);
    }

    @Override
    public List<SysHealthProject> listByPlanId(SysHealthProject project) {
        return null;
    }

    private void buildRecords(List<SysHealthProject> records) {
        if (!CollectionUtils.isEmpty(records)) {
            records.forEach(record -> {
                record.setGradeName(GradeUtil.getGradeName(Integer.valueOf(record.getGradeId())));
                String name = ProjectUtil.getPrjName(record.getCode());
                record.setProjectName(StringUtils.hasLength(name) ? name : record.getProjectName());
            });
        }
    }
}
